/*
 *  $TEAM_NAME Space Trader
 */
package presenters;

import models.Approach;
import models.Game;
import models.Player;
import models.StarSystem;
import views.EncounterPanel;
import views.MapOverlayPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

/**
 * Handles the presentation of the system on 
 * the map so the user can click and warp to systems.
 * @author bradley
 * @version 1.0
 */
public class MapOverlayPresenter 
extends InteractivePresenter<MapOverlayPanel> 
implements ActionListener {
	/**
	 * field
	 */
	@SuppressWarnings("unused") //this can be taken off if the parent is used
	private final MapPresenter parent;
    
	/**
	 * field
	 */
	private final StarSystem system;
    
	/**
	 * field
	 */
	private final Player player;
    
	/**
	 * field
	 */
	private final Game game;

    /**
     * map overlay presenter
     * @param parent
     * @param system
     * @param player
     * @param game
     * @param view
     */
    public MapOverlayPresenter(MapPresenter parent, StarSystem system, 
    		Player player, Game game, MapOverlayPanel view){

    	//Set local variables
        this.parent = parent;
        this.player = player;
        this.game = game;
        this.system = view.getStarSystem();

        //Set the presenter to present the map overlay
        view.addMouseListener(this);
        view.addActionListener(this);
        super.setView(view);
    }

    /**
     * Warps the player to the chosen system.
     */
    public void warp() {
    	final Approach approach = new Approach(
    			this.system.getApproachType(), this.system, this.game); 
    	final EncounterPresenter encounterPresenter = new EncounterPresenter(
    			approach, new EncounterPanel(approach, null));
    	this.present(encounterPresenter);
        player.setLocation(system);
    }

    /**
     * Triggered when the player clicks the warp button. 
     * @param event - the click of the warp button
     */
    public void actionPerformed(ActionEvent event) {
        final Object source = event.getSource();

        if (source instanceof JButton) {
            final JButton button = (JButton) source;

            if ("Warp!".equals(button.getText())) {
            	this.player.getShip().useFuel(
            			this.system.distanceTo(player.getLocation()));
                warp();
            }
        }
    }

    /**
     * Sets the view 
     * @param view - the view to set
     */
    public void setView(MapOverlayPanel view) {
        view.addMouseListener(this);
        view.addActionListener(this);

        this.view = view;
    }
    
    /**
     * to string
     * @return string
     */
    public String toString() {
		return "";
	}
}

